/**
 * related to baiduMap.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-07
 */
var page = sm("do_Page");
var app = sm("do_App");
var hashdata = mm("do_HashData");

//andriod返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "地图"
})
header.refreshData();

var param = {};
var map = ui("do_BaiduMapView_1");
var dingwei=sm("do_BaiduLocation");
if (page.getData()) {
	var data = page.getData();
	map.setCenter({latitude:data.y, longitude:data.x});
	var mark1 = [{"id":"id1","latitude":data.y,"longitude":data.x,"url":"source://image/mark.png","info":data.address}];
	map.addMarkers({data:mark1});
	param.latitude = data.y;
	param.longitude = data.x;
} else {
	dingwei.start({model:"high",isLoop:false}); 
	//接收到定位/更新定位时候触发 。返回值描述 :{latitude:'纬度​'，longitude:'经度', address:'北京市海淀区'}
	dingwei.on("result",function(data,e){
		map.setCenter({latitude:data.latitude, longitude:data.longitude});
		var mark1 = [{"id":"id1","latitude":data.latitude,"longitude":data.longitude,"url":"source://image/mark.png","info":data.address}];
		map.addMarkers({data:mark1});
		param = data;
	});
}

map.on("touchMap",function(data, e){
	dingwei.reverseGeoCode(data.latitude, data.longitude, function(data1, e1) {
		map.removeMarker({ids:["id1"]});
		var mark1 = [{"id":"id1","latitude":data.latitude,"longitude":data.longitude,"url":"source://image/mark.png","info":data1.address}
		];
		map.addMarkers({data:mark1});
		param = data1;
		param.latitude = data.latitude;
		param.longitude = data.longitude;
	});
});

var btn = ui("do_Button_1");
btn.on("touch",function(){
	if (param) {
		app.closePage(param);
	}
});